package TableAdditions;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.ViewportUI;
import javax.swing.table.DefaultTableModel;

import Utility.Customer;
import Utility.DateString;
import Utility.Product;
import Utility.DatePicker;
import Utility.Promotion;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class BillAdditionMenu extends JFrame {

	public static JPanel contentPane;
	public static boolean personnelFlag=false;
	public static boolean dayFlag=false;
	
	public static String selectedPersonnel="",selectedDate="";
	public static String selectedPersonelId="";
	public static String selectedDay="";
	public static String selectedProduct="";
	
	
	String selectedProductId="";
	public static String[] destinationsInRoute={};
	public static String routeId="";
	int customerCounter=0;
	int productCounter=0;
	int promotionCounter=0;
	String[] customerNames;
	public static Product productArray[];
	public static Customer customerArray[];
	public static Promotion promotionArray[];
	int selectedLine;
	float specialPrice ;
	static String datePickerDate="";
	public static JLabel totalLabel;
	public static JLabel totalCashLabel;
	public static JLabel totalCreditLabel;
	public static int rowCounter=0;
	
	public static JPanel customerPanel = new JPanel();
	public static JPanel dataPanel = new JPanel();
	public static JPanel dataPanel1 = new JPanel();
	public static JPanel dataPanel2 = new JPanel();
	public static JPanel dataPanel3 = new JPanel();
	public static JPanel dataPanel4 = new JPanel();
	public static JPanel dataPanel5 = new JPanel();
	public static JPanel dataPanel6 = new JPanel();
	public static JPanel dataPanel7 = new JPanel();
	public static JPanel dataPanel8 = new JPanel();
	public static JPanel dataPanel9 = new JPanel();
	public static JPanel dataPanel10 = new JPanel();
	public static JPanel dataPanel11 = new JPanel();
	static int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
    static int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; 
    public static JScrollPane jsp=new JScrollPane(customerPanel,v,h);
    private static JTextField textField;
    public static String billdate="";
    public static DateString dateString = new DateString("","");
   
     
    
	 
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BillAdditionMenu frame = new BillAdditionMenu();
					frame.setResizable(false);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BillAdditionMenu() {
		
		////System.out.println("in billAddition!!!!!!!!!!!!!!!!!!!!!!!!!!");
		this.setResizable(false);
		
		personnelFlag=false;
		dayFlag=false;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 1200, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		customerPanel.setBounds(50, 86, 1200,400);
		dataPanel.setBounds(50, 65, 1100,20);
		dataPanel1.setBounds(50, 65, 280,20);
		dataPanel2.setBounds(400, 65,100,20);
		dataPanel3.setBounds(525, 65,100,20);
		dataPanel4.setBounds(707, 65,34,20);
		dataPanel5.setBounds(752, 65,44,20);
		dataPanel6.setBounds(800, 65,44,20);
		dataPanel7.setBounds(850, 65,44,20);
		dataPanel8.setBounds(900, 65,44,20);
		dataPanel9.setBounds(960, 65,44,20);
		dataPanel10.setBounds(1020, 65,24,20);
		dataPanel11.setBounds(1063, 65,80,20);
		
	    customerPanel.setBackground(Color.white);
	    
	    dataPanel.removeAll();
	    dataPanel1.removeAll();
		dataPanel2.removeAll();
		dataPanel3.removeAll();
		dataPanel4.removeAll();
		dataPanel5.removeAll();
		dataPanel6.removeAll();
		dataPanel7.removeAll();
		dataPanel8.removeAll();
		dataPanel9.removeAll();
		dataPanel10.removeAll();
		dataPanel11.removeAll();
	    
	   
	    
	    customerPanel.setLayout(new GridBagLayout());
	    dataPanel2.setLayout(new GridLayout(0,1));
	    dataPanel3.setLayout(new GridLayout(0,1));
	    dataPanel9.setLayout(new GridLayout(0,1));
	    dataPanel10.setLayout(new GridLayout(0,1));
	    //customerPanel.add(Box.createVerticalStrut(10));
	    contentPane.repaint();
	    contentPane.revalidate();
	    jsp.setBounds(50, 86,1100,400);
	    JViewport vport = new JViewport();
	   // vport.set
	    contentPane.add(jsp);
	    //contentPane.add(dataPanel);
	    contentPane.add(dataPanel1);
	    contentPane.add(dataPanel2);
	    contentPane.add(dataPanel3);
	    contentPane.add(dataPanel4);
	    contentPane.add(dataPanel5);
	    contentPane.add(dataPanel6);
	    contentPane.add(dataPanel7);
	    contentPane.add(dataPanel8);
	    contentPane.add(dataPanel9);
	    contentPane.add(dataPanel10);
	    contentPane.add(dataPanel11);
	    
	   
	   
	    
	    
	    
	    
this.addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent arg0) {
				// TODO Auto-generated method stub
				customerPanel.removeAll();
				
			}
			
			@Override
			public void windowClosed(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	    

	    
	    
	    
	    
	    JLabel lblMteri = new JLabel("Müşteri");
	    //lblMteri.setBounds(310, 70, 70, 15);
	    //addItem(dataPanel2,lblMteri,0, 0,1, 1, GridBagConstraints.WEST,20);
        dataPanel2.add(lblMteri);
        
        JLabel lblrnAd = new JLabel("Ürün Adı");
        //lblrnAd.setBounds(500, 70, 70, 15);
        dataPanel3.add(lblrnAd);
        
        JLabel lblNakit = new JLabel("Fiyat");
        //lblNakit.setBounds(760, 70, 70, 15);
        dataPanel4.add(lblNakit);
        
        JLabel lblMiktar = new JLabel("Adet");
        //lblMiktar.setBounds(710, 70, 70, 15);
        dataPanel5.add(lblMiktar);
        
        JLabel lblProAmount = new JLabel("Pro.M.");
        //lblProAmount.setBounds(805, 70, 70, 15);
        dataPanel6.add(lblProAmount);
        
        JLabel lblTotalPro= new JLabel("T.Pro.");
        //lblTotalPro.setBounds(860, 70, 70, 15);
        dataPanel7.add(lblTotalPro);
        
        JLabel lblToplam = new JLabel("Top.");
        //lblToplam.setBounds(915, 70, 70, 15);
        dataPanel8.add(lblToplam);
        
        	            
        JLabel lblVt = new JLabel("VT");
        //lblVt.setBounds(960, 69, 70, 15);
        dataPanel9.add(lblVt);
        
        JLabel lblNt = new JLabel("NT");
        //lblNt.setBounds(1015, 70, 70, 15);
        dataPanel10.add(lblNt);
        
        JLabel lblNakitTutar = new JLabel("Nakit Tutar");
        //lblNakitTutar.setBounds(1070, 70, 109, 15);
        dataPanel11.add(lblNakitTutar);
	    
		
		JComboBox personnelComboBox = new JComboBox();
		personnelComboBox.setBounds(50, 11, 176, 20);
		contentPane.add(personnelComboBox);
		
		//////////System.out.println("Ready to execute sql");
		 	Connection con = null;
	        Statement st = null;
	        ResultSet rs = null;

	        String url = "jdbc:mysql://localhost:3306/mydb";
	        String user = "root";
	        String password = "qweasdzxc";

	        try {
	     
	            con = (Connection) DriverManager.getConnection(url, user, password);
	            st = (Statement) con.createStatement();
	            //st.executeUpdate("INSERT INTO job " + "VALUES (1, 'deneme')");
	            //st.executeUpdate("INSERT INTO special_price (special_price_customer_id,special_price_product_id,special_price) " + 
	            	//	"VALUES ('"+cidField.getText()+"'" +",'"+pidField.getText()+"','"+spField.getText()+"')");
	           //st.executeUpdate("ALTER TABLE special_price AUTO_INCREMENT = 1");
	            
	            
	            // for getting customer names from database
	            rs=st.executeQuery("SELECT * from customer WHERE activity='1'");
	            
	            while(rs.next()){
	            	customerCounter++;
	            }
	            
	            if(customerCounter>0){
			            customerArray = new Customer[customerCounter];
			            rs.first();
			            customerArray[0]=new Customer();
			            customerArray[0].setCustomerName(rs.getString("customer_name"));
			            customerArray[0].setCustomerCode(rs.getString("customer_code"));
			            customerArray[0].setCustomerId(rs.getInt("customer_id"));
	            
			            int count=1;
	            
			            while(rs.next()){
		            	 customerArray[count]=new Customer();
		            	 customerArray[count].setCustomerName(rs.getString("customer_name"));
		            	 customerArray[count].setCustomerCode(rs.getString("customer_code"));
		            	 customerArray[count].setCustomerId(rs.getInt("customer_id"));
		            	 count++;
			            }
	            
	            }
	            
	         // for getting product names from database
	            rs=st.executeQuery("SELECT * from promotion");
	            
	            while(rs.next()){
	            	promotionCounter++;
	            }
	            
	            if(promotionCounter>0){
	            
			            promotionArray=new Promotion[promotionCounter];
			            promotionArray[0]=new Promotion();
			            rs.first();
			            promotionArray[0].setPromotionId(rs.getInt("promotion_id"));
			            promotionArray[0].setPromotionProductId(rs.getInt("promotion_product_id"));
			            promotionArray[0].setPromotionCustomerId(rs.getInt("promotion_customer_id"));
			            promotionArray[0].setPromotionUnit(rs.getInt("promotion_unit"));
			            
			            //productArray[0].SetLongProductName(rs.getString("product_manufacturer")+" "+rs.getString("product_name"));
			          
			           int  count=1;
			            
			            while(rs.next()){
			            	promotionArray[count]=new Promotion();
			            	promotionArray[count].setPromotionId(rs.getInt("promotion_id"));
			            	promotionArray[count].setPromotionProductId(rs.getInt("promotion_product_id"));
				            promotionArray[count].setPromotionCustomerId(rs.getInt("promotion_customer_id"));
				            promotionArray[count].setPromotionUnit(rs.getInt("promotion_unit"));
			            	//productArray[count].SetLongProductName(rs.getString("product_manufacturer")+" "+rs.getString("product_name"));
			            
			            	count++;
			            }
			            
	            }
	            
	            for(int i=0;i<productCounter;i++){
	            	//////////System.out.println("productName= "+productNames[i]);
	            	//////////System.out.println("longProductName= "+longProductNames[i]);     	
	            }
	            
	            
	            // TG 28-11-2012
	            // rs=st.executeQuery("SELECT * from product WHERE activity='1'");
	            rs=st.executeQuery("SELECT * from view_product_ordered_by_name");
	            while(rs.next()){
	            	productCounter++;
	            }
	            
	            
	            
	            if(productCounter>0){
	            
			            productArray=new Product[productCounter];
			            productArray[0]=new Product();
			            rs.first();
			            productArray[0].setProductName(rs.getString("product_name"));
			            productArray[0].setProductCode(rs.getString("product_code"));
			            productArray[0].setProductId(rs.getInt("product_id"));
			            //productArray[0].SetLongProductName(rs.getString("product_manufacturer")+" "+rs.getString("product_name"));
			          
			            int count=1;
			            
			            while(rs.next()){
			            	productArray[count]=new Product();
			            	productArray[count].setProductName(rs.getString("product_name"));
				            productArray[count].setProductCode(rs.getString("product_code"));
				            productArray[count].setProductId(rs.getInt("product_id"));
			            	//productArray[count].SetLongProductName(rs.getString("product_manufacturer")+" "+rs.getString("product_name"));
			            
			            	count++;
			            }
	            
	            }
	            
	            for(int i=0;i<productCounter;i++){
	            	//////////System.out.println("productName= "+productNames[i]);
	            	//////////System.out.println("longProductName= "+longProductNames[i]);     	
	            }
	            
	            
	            //st.executeUpdate("DELETE FROM special_price");
	            rs=st.executeQuery("SELECT * from personel WHERE activity='1'");
	            personnelComboBox.addItem("Personel Adı");
	            
	            
	            while(rs.next())
	            {
	            	personnelComboBox.addItem(rs.getString("personel_name"));
	            }
	            
	            
            	personnelComboBox.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent event) {
						// TODO Auto-generated method stub
						

	                    JComboBox comboBox = (JComboBox) event.getSource();
	     
	                    //
	                    // Print the selected items and the action command.
	                    //
	                    Object selected = comboBox.getSelectedItem();
	                    //////////System.out.println("Selected Item  = " + selected);
	                    String command = event.getActionCommand();
	                    //////////System.out.println("Action Command = " + command);
	                    
	                    selectedPersonnel=(String) selected;
	                    //////////System.out.println("selectedPersonnel= "+selectedPersonnel);
	                    
	                    if(selectedPersonnel.equals("Personel AdÄ±")){
	                    	
	                    	
	                    	personnelFlag=false;
	                    }
	                    else{
	                    	personnelFlag=true;
	                    	Statement stmt = null;
		                	Connection conn = null;
		                	ResultSet rss = null;
		                	String url = "jdbc:mysql://localhost:3306/mydb";
		        	        String user = "root";
		        	        String password = "qweasdzxc";
		        	        
		        	        try {
								conn = (Connection) DriverManager.getConnection(url, user, password);
								stmt = (Statement) conn.createStatement();
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
		    	          
							
							 try {
									rss=stmt.executeQuery("SELECT * from personel where personel_name='"+selectedPersonnel+"' and activity='1'");
									while(rss.next()){
										selectedPersonelId=rss.getString("personel_id");
									}
									
								conn.close();	
									//////////System.out.println("SelectedPersonel id= "+selectedPersonelId);
								} catch (SQLException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
	                    	
	                    }
	                    
	                    if(dayFlag && personnelFlag){
	                    	customerPanel.removeAll();
	                    	customerPanel.repaint();
	                    	refreshDestinationsInRoute();
	            		}
	                   
	            
	                  
	                    // '"+cidField.getText()+"'
	                    
	                    //
	                    // Detect whether the action command is "comboBoxEdited"
	                    // or "comboBoxChanged"
	                    //
	                    if ("comboBoxEdited".equals(command)) {
	                        //////////System.out.println("User has typed a string in " +
	                          //      "the combo box.");
	                    } else if ("comboBoxChanged".equals(command)) {
	                        //////////System.out.println("User has selected an item " +
	                          //      "from the combo box.");
	                    }
	                    
	                    
	                    if(dayFlag && personnelFlag){
	                    	//////////System.out.println("fill table");
	                    	
	                    	
		                    	customerPanel.removeAll();
		                    	customerPanel.repaint();
		                    	refreshDestinationsInRoute();
		                    	
	                    	
	                    }
					
					
	                
						
					}
            		
            	});
	
	            rs=st.executeQuery("SELECT * from product WHERE activity='1'");
	            while(rs.next()){
	            	rowCounter++;
	            }
	            //////////System.out.println("Number Of Products = "+rowCounter);
	            
	            rs=st.executeQuery("SELECT * from product WHERE activity='1'");
	            
	            JButton btnAdd = new JButton("Ekle");
	            btnAdd.setVisible(false);
	            btnAdd.addMouseListener(new MouseAdapter() {
	            	@Override
	            	public void mouseClicked(MouseEvent e) {
	            		
	            		if(dayFlag && personnelFlag){
	            				SpecialRouteAdditionMenu frame = new SpecialRouteAdditionMenu(selectedPersonnel,billdate);
	            				frame.setVisible(true);
	            		}
	            		
	            	}});
	            btnAdd.setBounds(50, 490, 117, 25);
	            contentPane.add(btnAdd);
	            
	            
	            
	            JLabel lblVt_1 = new JLabel("VT:");
	            lblVt_1.setBounds(942, 496, 70, 15);
	            contentPane.add(lblVt_1);
	            
	            totalCreditLabel = new JLabel("0");
	            totalCreditLabel.setBounds(970, 496, 70, 15);
	            contentPane.add(totalCreditLabel);
	            
	            JLabel lblNt_1 = new JLabel("NT:");
	            lblNt_1.setBounds(812, 496, 70, 15);
	            contentPane.add(lblNt_1);
	            
	            totalCashLabel = new JLabel("0");
	            totalCashLabel.setBounds(840, 496, 70, 15);
	            contentPane.add(totalCashLabel);
	            
	            JLabel lblToplam_1 = new JLabel("Toplam:");
	            lblToplam_1.setBounds(1040, 496, 70, 15);
	            contentPane.add(lblToplam_1);
	            
	            totalLabel = new JLabel("0");
	            totalLabel.setBounds(1100, 496, 70, 15);
	            contentPane.add(totalLabel);
	            
	            textField = new JTextField();
	            textField.setEditable(false);
	            textField.setBounds(270, 12, 100, 20);
	            contentPane.add(textField);
	            textField.setColumns(10);
	            final JFrame startFrame = new JFrame();
	            
	            JButton dateButton = new JButton("Tarih");
	            dateButton.addActionListener(new ActionListener() {
	            	public void actionPerformed(ActionEvent e) {
	            		dateString=new DatePicker(startFrame).setDetailedPickedDate();
	            		textField.setText(dateString.getDate());
	            		
	            		if(!textField.getText().equals("")){
	            			dayFlag=true;
	            		}
	            	    //System.out.println("SelectedDay="+dateString.getDay());
	            		//contentPane.removeAll();
	            		
	            		if(dayFlag && personnelFlag){
	            			customerPanel.removeAll();
	            			FillBillAction.selectedRadioButton=0;
	            			FillBillAction.exSelectedRadioButton=0;
	            			//FillBillAction.fillBillPage(rowCounter,jsp,contentPane,productArray,customerArray,destinationsInRoute,promotionArray);
	            			refreshDestinationsInRoute();
	            		}
	            	
	            	}
	            });
	            dateButton.setBounds(401, 11, 80, 20);
	            contentPane.add(dateButton);
	            
	            
	           con.close();

	        } catch (SQLException ex) {
	            Logger lgr = Logger.getLogger(main.class.getName());
	            lgr.log(Level.SEVERE, ex.getMessage(), ex);

	        } finally {
	            try {
	                if (rs != null) {
	                    rs.close();
	                }
	                if (st != null) {
	                    st.close();
	                }
	                if (con != null) {
	                    con.close();
	                }
	                
	            } catch (SQLException ex) {
	                Logger lgr = Logger.getLogger(main.class.getName());
	                lgr.log(Level.WARNING, ex.getMessage(), ex);
	            }
	        }
		
	}
	
	
	protected static String arrangeDate(String text) {
		String arrangedDate=text;
		int dashCounter=0;
		String day="";
		String month="";
		String year="";
		String temp="";
		
		for(int b=0;b<arrangedDate.length();b++){
			char tmp= arrangedDate.charAt(b);
			if(tmp=='-'){
				if(dashCounter==0){
					day=temp;
					temp="";
				}
				if(dashCounter==1){
					month=temp;
					temp="";
				}
				dashCounter++;
			}
			else{
				temp=temp+tmp;
			}
			year=temp;
		}
		
		arrangedDate=year+"-"+month+"-"+day;
		
		return arrangedDate;
	}

	public static void deleteAllRows(final DefaultTableModel model) {
	    for( int i = model.getRowCount() - 1; i >= 0; i-- ) {
	        model.removeRow(i);
	    }
	}
	
	private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align,int ipady) {
        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.ipady=ipady;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets = new Insets(3,0,3,0);
        gc.anchor = align;
        gc.fill = GridBagConstraints.HORIZONTAL;
        p.add(c, gc);
      }
	
	public static String getDate(){
		Calendar cal = Calendar.getInstance();
		 int day = cal.get(Calendar.DATE);
	     int month = cal.get(Calendar.MONTH) + 1;
	     int year = cal.get(Calendar.YEAR);
	     
	    String temp1=Integer.toString(month); 
	    if(month<10){
	    	temp1="0"+temp1;
	    	//month=Integer.parseInt(temp);
	    }
	    
	    String temp2=Integer.toString(day);;
	    
	    if(day<10){
	    	temp2="0"+temp2;
	    	//day=Integer.parseInt(temp);
	    }
	   
	    billdate = year+"-"+temp1+"-"+temp2;
	    //////////System.out.println("edited text= "+billdate);
	    
	    datePickerDate=arrangeDate(textField.getText());
	    //////////System.out.println("datepicker="+datePickerDate);
	    
	    if(!datePickerDate.equals("--")){
	    	billdate=datePickerDate;
	    }
		return billdate;
	}
	
	
	
	
	
	public static void refreshDestinationsInRoute(){
		
		System.out.println("in refresh destinations!!!!!!!!!!!!!!!!!!!!!!!!");
		customerPanel.removeAll();
		Statement stmt = null;
    	Connection conn = null;
    	ResultSet rss = null;
    	String url = "jdbc:mysql://localhost:3306/mydb";
        String user = "root";
        String password = "qweasdzxc";
        
        try {
			conn = (Connection) DriverManager.getConnection(url, user, password);
			stmt = (Statement) conn.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        
		
		billdate=getDate();
		////System.out.println("billDate!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!="+billdate);
		String tempBillDate=arrangeDate(textField.getText());
		////System.out.println("tempBillDate!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!="+tempBillDate);
    	routeId=selectedPersonnel+"/"+tempBillDate;
    	//System.out.println("routeId!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!="+routeId);
    	
    	try {
			rss=stmt.executeQuery("SELECT destination_id from  special_route_destination_list WHERE route_id='"+routeId+"' ORDER BY destination_id");
			
			
			 rowCounter=0;
	    	 while(rss.next()){
	    		 //String route_id=rss.getString("destination_id");
	    		 //////////System.out.println("route_id= "+route_id);
	    		 rowCounter++;
	    	 }
	    	
	    	 
	    	 //System.out.println("rowCounter="+rowCounter);
	    	 
	    	if(rowCounter==0){
	    		System.out.println("row counter=0");
	    		System.out.println("day= "+dateString.getDay());
	    		routeId=selectedPersonnel+"/"+dateString.getDay();
	    		System.out.println("new  routeId!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!="+routeId);
	    		rss=stmt.executeQuery("SELECT destination_id from  route_destination_list WHERE route_id='"+routeId+"' ORDER BY destination_id");
	    		
	    		rowCounter=0;
	        	 while(rss.next()){
	        		 //String route_id=rss.getString("destination_id");
	        		 //////////System.out.println("route_id= "+route_id);
	        		 rowCounter++;
	        	 }
	        	 
	        	 System.out.println("new rowcounter="+rowCounter);
	    		
	    		
	    	}
	    	
	    		                    	
	    	 
	    	 if(rowCounter>0){
	    		 System.out.println("billdate!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!="+billdate);
	    		 destinationsInRoute =new String[rowCounter];
	    		 rowCounter=0;
	    		 rss.first();
	    		 destinationsInRoute[rowCounter]=rss.getString("destination_id");
	    		 rowCounter++;
	    		 while(rss.next()){
	    			 destinationsInRoute[rowCounter]=rss.getString("destination_id");
	    			 rowCounter++;
	    		 }
	    		 
	    	 
	    	 }
	    	 
	    	 
	    	 customerPanel.removeAll();
    		 FillBillAction.fillBillPage(rowCounter,jsp,contentPane,productArray,customerArray,destinationsInRoute,promotionArray);
    		 
	    	
	    	//rss=stmt.executeQuery("SELECT * from  route_destination_list WHERE route_id='"+routeId+"'");
	    	
	    	conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

